import {Component, OnDestroy, OnInit, ViewEncapsulation} from '@angular/core';

import {Subscription} from "rxjs";
import {Router, ActivatedRoute, Params} from "@angular/router";
import {ToolsService} from "@shared/service/tools/tools.service";
import {AuthService} from "@shared/service/auth/auth.service";
import {ImageServeService} from "@shared/service/image/image-serve.service";
import {MineService} from "@mine/mine.service";
import {imageUtil} from "@core/globals/globals.service";
import {Observable} from "rxjs/Observable";
import {Subject} from "rxjs/Subject";
import {UserInfoVO} from "@interface/index";


@Component({
  selector: 'seed-mine',
  templateUrl: './mine.component.html',
  styleUrls: ['./mine.component.scss'],
  encapsulation: ViewEncapsulation.None,
})
export class MineComponent implements OnInit, OnDestroy {
  public imageUtil = imageUtil.path;
  public subscription: Subscription = new Subscription();
  public ngUnsubscribe: Subject<void> = new Subject<void>();
  public userProfile$: Observable<UserInfoVO>;
  private userId: string;
  private _concern: number | string;
  private _follow: number | string;
  private _stayPay: number | string;
  private _stayComplete: number | string;
  public serviceMenuList: any = [];

  constructor(public router: Router,
              public route: ActivatedRoute,
              public mineService: MineService,
              public toolsService: ToolsService,
              public auth: AuthService,
              public imgService: ImageServeService) {
    this.userProfile$ = this.mineService.fetchUserPorfile(this.route);
    this.userProfile$.subscribe(profile => {
      this.userId = profile.userId;
    });

  }

  /**
   * @description 关注数量 geter & seter
   * @author @chenwh576
   * @date 20171211
   * @return {number | string}
   */
  get concern(): number | string {
    return this._concern;
  }

  setConcern(value: number | string = 0) {
    const num = value ?
      value > 999 ? '999+' : value
      : 0;
    this._concern = num;
  }

  /**
   * @description 粉丝数量 geter & seter
   * @author @chenwh576
   * @date 20171211
   * @return {number | string}
   */
  get follow(): number | string {
    return this._follow;
  }

  setFollow(value: number | string = 0) {
    const num = value ?
      value > 999 ? '999+' : value
      : 0;
    this._follow = num;
  }

  /**
   * @description 待付款数量 geter & seter
   * @author @baoww765
   * @date 2017-12-14
   * @return {number | string}
   */
  get stayPay(): number | string {
    return this._stayPay ? this._stayPay : 0;
  }

  setStayPay(value) {
    for (const key in value) {
      if (key === '1') {
        this._stayPay = value[key];
      }
    }
  }

  /**
   * @description 待完善数量 geter & seter
   * @author @baoww765
   * @date 2017-12-14
   * @return {number | string}
   */
  get stayComplete(): number | string {
    return this._stayComplete ? this._stayComplete : 0;
  }

  setstayComplete(value) {
    for (const key in value) {
      if (key === '9') {
        this._stayComplete = value[key];
      }
    }
  };

  complete(data) {
    return data[9] > 0 ? true : false;
  };

  pay(data) {
    return data[1] > 0 ? true : false;
  };

  goToWallet() {
    this.router.navigate(['/mine/wallet']);
  }

  goToMore() {
    this.router.navigate(['/mine/more']);
  }

  goToOrders(tabCode: string = 'all') {
    this.router.navigate(['/order', tabCode]);
    // tabCode?this.router.navigate(['/order'], {queryParams: { tabCode : tabCode}}):this.router.navigate(['/order']);
  }

  /*
   * 添加众筹的功能
   * */
  goToCrowds() {
    this.router.navigate(['/crowd/my-crowd']);
  }

  /*转跳关注,收藏，粉丝页面*/
  goToFanslist() {
    this.router.navigate([`/homepage/fans`], {queryParams: {userId: this.userId}});
  }

  goToCollectlist() {
    this.router.navigate([`/mine/collect`]);
  }

  goToFollowlist() {
    this.router.navigate([`/homepage/follows`], {queryParams: {userId: this.userId}});
  }

// 跳转编辑个人资料
  goToSetting() {
    this.router.navigate(['./mine/setting']);
  }

  /**
   * @description /获取推荐服务菜单列表
   * @author chensk740
   * @date 2018-04-03
   */
  getServiceMenu() {
    this.mineService.getServiceMenu().subscribe(res => {
      console.log(res);
      if (res.result === '0' && res.data) {
        this.serviceMenuList = res.data;
      }
    });
  }

  goToHomepage() {
    this.router.navigate(['/', this.userId]);
  }

  goKeFu() {
    location.href = 'https://www.71chat.com/scsf/phone/visitor.html?cmpcd=106282';
  }

  /**
   * @description 获取图标
   * @param iconName 图标名
   * @author baoww765
   * @date 2017-12-13
   */
  getIcon(iconName: string) {
    return this.imgService.getIcon(iconName);
  }

  getImage(name: string): string {
    return this.imgService.getImage(name);
  }

  /**
   * @description 前往我的优惠券列表
   * @author ouyqx691
   * @date 2017-12-26
   */
  goToCoupon() {
    this.router.navigate(['/goods/coupons/self', this.userId]);
  }

  /**
   * @description 前往推荐服务详情页
   * @author chensk740
   * @date 2018-04-03
   */
  goServiceDetail(url) {
    if (url) {
      this.router.navigateByUrl(url);
    } else {
      this.toolsService.presentConfirm('下载求苗APP，看更多精彩内容!');
    }

  }

  ngOnInit() {
    this.getServiceMenu();
  }

  ngOnDestroy() {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
    this.toolsService.hideLoading();
  }

}
